package net.maku.kyy.service.impl;

import lombok.AllArgsConstructor;
import net.maku.framework.security.user.UserDetail;
import net.maku.kyy.dao.KyyUserRoleDao;
import net.maku.kyy.enums.KyyDataScopeEnum;
import net.maku.kyy.enums.KyyUserStatusEnum;
import net.maku.kyy.service.KyyUserDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * KYY用户 UserDetails 信息
 */
@Service
@AllArgsConstructor
public class KyyUserDetailsServiceImpl implements KyyUserDetailsService {
    @Autowired
    private KyyUserRoleDao kyyUserRoleDao;


    @Override
    public UserDetails getUserDetails(UserDetail userDetail) {
        // 账号不可用
        if (userDetail.getStatus() == KyyUserStatusEnum.DISABLE.getValue()) {
            userDetail.setEnabled(false);
        }
        
        // 设置用户权限列表，这里可以根据实际需求从数据库获取
        // 初始化一个空的权限集合
        Set<String> authoritySet = new HashSet<>();
        
        // 添加基本权限
        authoritySet.add("kyy:user");
        
        // 设置权限集合
        userDetail.setAuthoritySet(authoritySet);
        
        // 数据权限范围
//        List<Long> dataScopeList = getDataScope(userDetail);
//        userDetail.setDataScopeList(dataScopeList);

        return userDetail;
    }


    /**
     * 获取数据权限
     */
    private List<Long> getDataScope(UserDetail userDetail) {
        // 如果用户ID为空，直接返回空列表避免空指针
        if (userDetail.getId() == null) {
            return new ArrayList<>();
        }
        
        Integer dataScope = kyyUserRoleDao.getDataScopeByUserId(userDetail.getId());
        if (dataScope == null) {
            return new ArrayList<>();
        }

        if (dataScope.equals(KyyDataScopeEnum.ALL.getValue())) {
            // 全部数据权限，则返回null
            return null;
        }
//        else if (dataScope.equals(KyyDataScopeEnum.ORG_AND_CHILD.getValue())) {
//            // 本机构及子机构数据
////            List<Long> dataScopeList = sysOrgService.getSubOrgIdList(userDetail.getOrgId());
//            // 自定义数据权限范围
//            dataScopeList.addAll(kyyUserRoleDao.getDataScopeList(userDetail.getId()));
//
//            return dataScopeList;
//        }
        else if (dataScope.equals(KyyDataScopeEnum.ORG_ONLY.getValue())) {
            // 本机构数据
            List<Long> dataScopeList = new ArrayList<>();
            dataScopeList.add(userDetail.getOrgId());
            // 自定义数据权限范围
//            dataScopeList.addAll(sysRoleDataScopeDao.getDataScopeList(userDetail.getId()));

            return dataScopeList;
        }
//        else if (dataScope.equals(KyyDataScopeEnum.CUSTOM.getValue())) {
//            // 自定义数据权限范围
//            return sysRoleDataScopeDao.getDataScopeList(userDetail.getId());
//        }

        return new ArrayList<>();
    }
} 